package org.jeecg.modules.login.handler.wechat;

import lombok.Data;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;

/**
 * 企业微信执行器
 *
 * @author tianyi.jiang
 * @since 1.0.0
 */
@Slf4j
@Component
@ConditionalOnExpression("'${basicplatform.wechat.enterprise.enable:false}'.equals('true')")
public class WeChatEnterpriseHandler {

    private final WeChatEnterpriseProperty weChatEnterpriseProperty;

    /**
     * 授权认证并获取微信用户信息
     *
     * @param code 授权码
     * @return 微信用户信息
     */
    @SneakyThrows
    public WxCpOauth2UserInfo weChatUser(@Nonnull String code) {
        WxCpOauth2UserInfo wxCpOauth2UserInfo = weChatEnterpriseProperty.getWxCpService().getOauth2Service().getUserInfo(code);
        log.info("[WeChat] Oauth2 response: {}", wxCpOauth2UserInfo.toString());
        return wxCpOauth2UserInfo;
    }

    /**
     * 发送文字消息
     *
     * @param weChatId 企业微信用户Id
     * @param text     文本
     */
    @SneakyThrows
    public void send(@Nonnull String weChatId, @NonNull String text) {
        WxCpMessage wxCpMessage = WxCpMessage.TEXT().agentId(weChatEnterpriseProperty.getAgentId()).toUser(weChatId).content(text).build();
        WxCpMessageSendResult wxCpMessageSendResult = weChatEnterpriseProperty.getWxCpService().getMessageService().send(wxCpMessage);
        log.info("[WeChat] Send message api response: {}", wxCpMessageSendResult.toString());
    }

    /**
     * 获取企业微信用户ID
     *
     * @param mobile 电话号码
     * @return 企业微信用户ID
     */
    @SneakyThrows
    public String weChatIdByPhone(@Nonnull String mobile) {
        return weChatEnterpriseProperty.getWxCpService().getUserService().getUserId(mobile);
    }

    public WeChatEnterpriseHandler(WeChatEnterpriseProperty weChatEnterpriseProperty) {
        this.weChatEnterpriseProperty = weChatEnterpriseProperty;
    }

    @Data
    @Configuration
    @ConfigurationProperties(prefix = "basicplatform.wechat.enterprise")
    public static class WeChatEnterpriseProperty {
        /**
         * 授权后重定向认证地址
         */
        private String authUrl;
        /**
         * 企业Id
         */
        private String corpId;
        /**
         * 应用Id
         */
        private Integer agentId;
        /**
         * 应用秘钥
         */
        private String secret;

        public WxCpService getWxCpService() {
            WxCpDefaultConfigImpl wxCpDefaultConfigImpl = new WxCpDefaultConfigImpl();
            wxCpDefaultConfigImpl.setCorpId(this.getCorpId());
            wxCpDefaultConfigImpl.setAgentId(this.getAgentId());
            wxCpDefaultConfigImpl.setCorpSecret(this.getSecret());
            WxCpService wxCpService = new WxCpServiceImpl();
            wxCpService.setWxCpConfigStorage(wxCpDefaultConfigImpl);
            return wxCpService;
        }
    }

}